<?php
namespace Admin\Controller;
class BannerController extends CheckController{

    //分配Article模板页面
    function index(){
        $id=isset($_GET['id'])?I('get.id'):'';
        $this->page_title="ئايلانما ئېلان باشقۇرۇش";
        $where="ad.pid=cate.id";
        if($id){
            $where.=" and cate.id=".$id;
        }
        $bannerad=M("bannerad")
            ->table("dp_bannerad ad , dp_bannercate cate ")
            ->where($where)
            ->field("ad.id,ad.sort, ad.thumb,ad.intime,ad.uptime,ad.status,cate.name,ad.adtitle")
            ->order("sort desc")
            ->select();
        $this->assign('bannerad',$bannerad);
        //广告片分类
        $bannercate=_sort(M("bannercate")->select());
        $this->assign('bannercate',$bannercate);
        $this->assign('get_id',$id);
        $this->display("index");
    }
    //分配页面
    public function add(){
        $this->page_title=" ئايلانما ئېلان قوشۇش";
        $classify=_sort(M("bannercate")->select());
        $this->assign('classify',$classify);
        $this->display("add");
    }
    //分配页面
    public function addcate(){
        $this->page_title=" ئايلانما ئېلان سەھىپە قوشۇش";
        $this->display("addcate");
    }

    //接收数据并且写入到数据库
    public function addhand(){
        //实例化对象
        $user = D("bannerad");
        if (!$user->create()){//如果创建数据对象失败，就提示
            $this->error($user->getError());
        }else{
            //新增的数据id
            $aid=$user->add();
            //手动添加修改时间
            $uptime=array(
                "uptime"=>date("Y-m-d H:i:s"),
                "intime"=>date("Y-m-d H:i:s")
            );
            M('bannerad')->where("id='$aid'")->save($uptime);
            if($aid){
                $this->display("../index");
                _check_alert("ئېلان قوشۇش مۇۋاپىقيەتلىك بولدى !","index.html",1);
            }
        }
    }

    //接收数据并且写入到数据库
    public function addcatehend(){
        if(stripos(trim(I('post.name')),"،")){
            $post_data['names']=explode("،",I('post.name'));
        }elseif (stripos(trim(I('post.name')),",")){
            $post_data['names']=explode(",",I('post.name'));
        }elseif (stripos(trim(I('post.name')),"  ")){
            $post_data['names']=explode("  ",I('post.name'));
        }elseif(stripos(trim(I('post.name'))," ")){
            $post_data['names']=I('post.name');
        }elseif(is_numeric(trim(I('post.name')))){
            $post_data['names']=I('post.name');
        }
        $post_data['sort']=I('post.sort');
        $post_data['pid']=I('post.pid');
        $post_data['status']=I('post.status');
        $rules = array(
            array('name','require','سەھىپە نامىنى چۇقۇم يېزىڭ！',1), //默认情况下用正则进行验证
            array('pid','require','ماس سەھىپە تاللاڭ',2)
        );
        if(!M("bannercate")->validate($rules)->create()){
            $this->error(M("bannercate")->getError());
        }else{
            $i=M("bannercate")->max('sort');
            if(is_array($post_data['names'])){
                foreach ($post_data['names'] as $name){
                    $name=htmlspecialchars(trim($name));
                    $i++;
                    $data=array(
                        'name'=>$name,
                        'sort'=>I('post.sort')+$i,
                        'pid'=>I('post.pid'),
                        'status'=>I('post.status')
                    );
                    $result=M("bannercate")->add($data);
                }
            }else{
                $result=M("bannercate")->add(I('post.'));
            }
            if($result){
                $this->display("../index");
                _check_alert("مۇۋاپىقىيەتلىك بولدى !","index.html",1);
            }
        }
    }

    //编辑分类
    public function edit(){
        $id=I('get.id');
        $this->page_title="ئايلانما ئېلان تەھرىرلەش";
        //获取要编辑的一条数据
        $bannerad=M("bannerad")->find($id);
        $this->assign("bannerad",$bannerad);
        //获取所有的分类
        $bannercate=M("bannercate")->select();
        $this->assign('bannercate',$bannercate);
        $this->display("edit");
    }
    //接收数据并且保存到数据库
    public function savehend(){
        $data['id']=I('post.id');
        $data['uptime']=date("Y-m-d H:i:s");
        $rules = array(
            array('pid','require','  سەھىپە تاللاڭ！',1),
            array('adurl','url',' توغرا بولغان ئۇلانما ئادېرىسنى يېزىڭ !',2),
            array('thumb','require',' رەسىم يوللاڭ！',1),
        );
        $User=M("bannerad");
        if(!$User->validate($rules)->create()){
            $this->error($User->geterror(),"",1);
        }else{
            $User->save($data);
            $result =$User->save(); // 保存到数据库
            if($result!==false){
                $this->display("../index");
                _check_alert(" ئۆزگەرتىش مۇۋاپىقيەتلىك بولدى !","index.html",1);
            }
        }
    }

//    //上传封面图片处理
    public function upload(){
        $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './Uploads/',
            'savePath'   =>    'ad/',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );

        $upload = new \Think\Upload($config);// 实例化上传类
        $info=$upload->upload();
        $thumb=$info['file'];
        $thumbPath='Uploads/'.$thumb['savepath'].$thumb['savename'];//图片的完美地址
        $photo['type']='banner_upload';
        $photo['thumb']='/'.$thumbPath;
//        //使用thumb方法生成缩略图
//        $image = new \Think\Image();
//        //打开图像资源
//        $image->open($thumbPath);
//        //裁剪图片
//        $image->thumb(150, 150)->save($thumbPath);
        if($info){
            $row=M("photo")->where("thumb='$thumbPath'")->select();
            if(!$row){
                M('photo')->add($photo);
            }
            $data=array(
                'status'=>1,
                'message'=>'رەسىم يوللاش تامام',
                'src'=>$thumbPath
            );
        }else{
            $data=array(
                'status'=>0,
                'message'=>'رەسىم يوللاش مەغلۇب بولدى !',
            );
        }
        $this->ajaxReturn($data);
    }

    //用ajax分类状态
    public function sortStatus(){
        $id=I('post.id');
        $post['status']=I('post.status');
        if(!M('bannerad')->where("id='$id'")->save($post)){
            $data=array("status"=>0,"content"=>"مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"content"=>"مۇۋاپقىيەتلىك بولدى !");
        }
        $this->ajaxReturn($data);
    }
    //用ajax删除分类
    public function del(){
        $id=I('post.id');
        //然后删除封面图片来源地址
        $thumb_link=M("bannerad")->where("id='$id'")->getField("thumb",true);
        //图片完美的地址
        $thumb=$thumb_link[0];
        //获取要删除的一条数据
        $data=M("bannerad")->delete($id);
        if(!$data){
            $data=array("status"=>0,"message"=>"ئۆچۈرۈش مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            //文章删除成功的话就闪烁出封面退片地址
            unlink($thumb);
            $data=array("status"=>1,"message"=>"ئۆچۈرۈش مۇۋاپىقيەتلىك بولدى !");
        };
        $this->ajaxReturn($data);
    }
    //用ajax排序分类
    public function sortfen(){
        $post=array();
        $id=I('post.id');
        $post['sort']=I('post.sort');
        //获取要编辑的一条数据
        $data=M("bannerad")->where("id='$id'")->save($post);
        if(!$data){
            $data=array("status"=>0,"message"=>"رەتلەش مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"message"=>"رەتلەش مۇۋاپىقيەتلىك بولدى !");
        };
        $this->ajaxReturn($data);
    }

}